package com.lovo.gui.Ddz;
import java.awt.Container;
import java.awt.Point;
import java.util.Random;

public class BBanker extends Thread {
    public Container table;
    private BCards[] card = new BCards[54];
    // 创建一个随机数对象
    private Random random = new Random();
    // 创建三个玩家
    BPlayer Player1 = new BPlayer();
    BPlayer Player2 = new BPlayer();
    BPlayer Player3 = new BPlayer();

    public BBanker(Container table) {
        this.table = table;
    }

    /*
     * 做庄函数
     */
    public void run() {
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 13; j++) {
                // 准备阶段，创建54个卡牌对象，卡牌分成五堆，放在桌面上
                if (i == 4 && j > 1) {
                    break;
                }
                BCards cards = new BCards(i + "-" + j);
                cards.setLocation(200 + i * 30, 30);
                table.add(cards);
                table.repaint();
                // 洗牌阶段，即每张牌随机存入数组
                shuffle(cards);
            }
        }
        // 发牌阶段
        deal();
    }

    /*
     * 洗牌的函数
     */
    public void shuffle(BCards cards) {
        int ran = random.nextInt(54);
        while (card[ran] != null) {
            ran = random.nextInt(54);
        }
        card[ran] = cards;
    }

    /*
     * 发牌的函数
     */
    public void deal() {
        int c = 0, f = 0, s = 0, t = 0;
        for (int i = 0; i < 51; i++) {
            // 发牌阶段，留下三张地主牌，并且是背面朝上。
            BCards cards = card[i];
            switch ((c++) % 3) {
                case 0:
                    CMoving.move(cards, cards.getLocation(), new Point(25, 50 + (f++) * 20));
                    Player1.linkedList.add(cards);
                    break;
                case 1:
                    CMoving.move(cards, cards.getLocation(), new Point(97 + (s++) * 20, 470));
                    Player2.linkedList.add(cards);
                    break;
                case 2:
                    CMoving.move(cards, cards.getLocation(), new Point(500, 50 + (t++) * 20));
                    Player3.linkedList.add(cards);
                    break;
            }
            cards.overturn();
            table.setComponentZOrder(cards, 0);
        }
    }
}